package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Buildinfo;
import com.ruoyi.system.domain.Floor;
import com.ruoyi.system.service.IBuildinfoService;
import com.ruoyi.system.service.IFloorService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Area;
import com.ruoyi.system.service.IAreaService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 区域信息Controller
 * 
 * @author yaozh
 * @date 2024-03-04
 */
@RestController
@RequestMapping("/system/area")
public class AreaController extends BaseController
{
    @Autowired
    private IAreaService areaService;

    @Autowired
    private IFloorService floorService;

    @Autowired
    private IBuildinfoService buildinfoService;

    /**
     * 查询区域信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:area:list')")
    @GetMapping("/list")
    public TableDataInfo list(Area area,@RequestParam String dbName)
    {
        Long[] appIds = buildinfoService.listAppIds(getLoginUser(),dbName);
        if(appIds.length > 0){

        }else{
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<Area> list = areaService.selectAreaList(appIds, area, dbName);
        return getDataTable(list);
    }

    /**
     * 导出区域信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:area:export')")
    @Log(title = "区域信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Area area,@RequestParam String dbName)
    {
        Long[] appIds = buildinfoService.listAppIds(getLoginUser(),dbName);
        List<Area> list = new ArrayList<>();
        if(appIds.length > 0){
            list = areaService.selectAreaList(appIds, area, dbName);
        }
        ExcelUtil<Area> util = new ExcelUtil<Area>(Area.class);
        util.exportExcel(response, list, "区域信息数据");
    }

    /**
     * 获取区域信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:area:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id,@RequestParam String dbName)
    {
        return success(areaService.selectAreaById(id, dbName));
    }

    /**
     * 新增区域信息
     */
    @PreAuthorize("@ss.hasPermi('system:area:add')")
    @Log(title = "区域信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Area area,@RequestParam String dbName)
    {
        return toAjax(areaService.insertArea(area, dbName));
    }

    /**
     * 修改区域信息
     */
    @PreAuthorize("@ss.hasPermi('system:area:edit')")
    @Log(title = "区域信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Area area,@RequestParam String dbName)
    {
        return toAjax(areaService.updateArea(area, dbName));
    }

    /**
     * 删除区域信息
     */
    @PreAuthorize("@ss.hasPermi('system:area:remove')")
    @Log(title = "区域信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids,@RequestParam String dbName)
    {
        return toAjax(areaService.deleteAreaByIds(ids, dbName));
    }

    /**
     * 查询楼层信息列表
     */
    @GetMapping("/floor")
    public AjaxResult listAllFloor(@RequestParam String dbName)
    {
        Long[] appIds = buildinfoService.listAppIds(getLoginUser(),dbName);
        if(appIds.length > 0){

        }else{
            return success(new ArrayList<>());
        }
        List<Floor> list = floorService.selectFloorList(appIds,new Floor(),dbName);
        return success(list);
    }
}
